package com.campus.lostandfound.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.campus.lostandfound.common.Result;
import com.campus.lostandfound.entity.Announcement;
import com.campus.lostandfound.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 公告Controller
 */
@RestController
@RequestMapping("/announcement")
@CrossOrigin
public class AnnouncementController {
    
    @Autowired
    private AnnouncementService announcementService;
    
    /**
     * 发布公告（管理员）
     */
    @PostMapping("/publish")
    public Result<String> publishAnnouncement(@RequestBody Announcement announcement) {
        try {
            announcementService.publishAnnouncement(announcement);
            return Result.success("发布成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取公告列表
     */
    @GetMapping("/list")
    public Result<IPage<Announcement>> getAnnouncementList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            IPage<Announcement> page = announcementService.getAnnouncementList(current, size);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取公告详情
     */
    @GetMapping("/{id}")
    public Result<Announcement> getAnnouncement(@PathVariable Long id) {
        try {
            Announcement announcement = announcementService.getById(id);
            if (announcement == null) {
                return Result.error("公告不存在");
            }
            return Result.success(announcement);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

